package com.gr3g.dev.image;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

/** @author Gregory Tardivel */
public final class ImageAnalyzer {

    public static StringBuilder parse(final File fImage)
            throws IOException {
        final StringBuilder builder = new StringBuilder();
        builder.append("{");
        builder.append("'image':[");
        final BufferedImage bImage = ImageIO.read(fImage);
        final List<String> points = new ArrayList<String>();
        for (int x = 0; x < bImage.getWidth(); x++) {
            for (int y = 0; y < bImage.getHeight(); y++) {
                final int rgb = bImage.getRGB(x, y);
                if (rgb != -1) {
                    points.add("{'x':" + x + ",'y':" + y + "}");
                }
            }
        }
        for (final String point : points) {
            builder.append("{");
            builder.append("'pixel':");
            builder.append(point);
            if (!point.equals(points.get(points.size() - 1))) {
                builder.append("},");
            } else {
                builder.append("}");
            }
        }
        builder.append("]");
        builder.append("}");
        return builder;
    }

}
